<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle">
      <span v-if="!fullLocation" class="placeholder">请选择配送地址</span>
      <span v-else class="value">{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
   <div class="option" v-if="isShow">
       <div v-show="!list.length" class="loading"></div>
      <span class="ellipsis" v-for="item in list" :key="item.code" @click="setAddress(item)">{{item.name}}</span>
    </div>
  </div>
</template>

<script>
import { computed, ref, reactive } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
/**
 * 获取城市列表
 */
const getCityList = async () => {
  if (window.cityList) {
    return window.cityList
  } else {
    const { data } = await axios.get('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
    window.cityList = data
    return window.cityList
  }
}
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const target = ref(null)
    const isShow = ref(false)
    const cityList = ref([])

    /**
     * 选中的地址信息合集
     */
    const address = reactive({
      provinceCode: '', // 省份code
      provinceName: '', // 省份名字
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })

    // 设置地址
    const setAddress = ({ code, level, name }) => {
      console.log(code, level, name)
      if (level === 0) {
        address.provinceCode = code
        address.provinceName = name
      } else if (level === 1) {
        address.cityCode = code
        address.cityName = name
      } else if (level === 2) {
        address.countyCode = code
        address.countyName = name
        // 地址
        address.fullLocation = address.provinceName + ' ' + address.cityName + ' ' + address.countyName
        // 关闭
        hide()
        emit('change', address)
      }
    }

    const show = async () => {
      isShow.value = true
      // 后面还有有其他业务

      /**
       * 城市列表
       */
      cityList.value = await getCityList()
    }

    const hide = () => {
      isShow.value = false
      // 后面还有有其他业务
    }

    const toggle = () => {
      isShow.value ? hide() : show()
    }

    // 点击目标元素之外  onClickOutside(目标元素,事件处理函数)
    onClickOutside(target, () => {
      console.log('你点击了元素外')
      hide()
    })

    const list = computed(() => {
      // 默认显示省份
      let currentLIst = cityList.value
      console.log(currentLIst)
      //   显示市
      if (address.provinceCode) {
        currentLIst = currentLIst.find(item => item.code === address.provinceCode).areaList
      }
      if (address.cityCode) {
        currentLIst = currentLIst.find(item => item.code === address.cityCode).areaList
      }
      return currentLIst
    })
    return { target, isShow, toggle, list, address, setAddress }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
       // 省略...
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
